package com.daka.service.impl;

import com.daka.dao.TopicCommentMapper;
import com.daka.dao.TopicMapper;
import com.daka.entity.Topic;
import com.daka.entity.TopicComment;
import com.daka.enumerate.TopicEnum;
import com.daka.service.TopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author beliveli on 2016/3/31 0031.
 */
@Service("topicService")
@SuppressWarnings("unused")
public class TopicServiceImpl implements TopicService {

    @Resource(name = "topicMapper")
    private TopicMapper topicMapper;

    @Resource(name = "topicCommentMapper")
    private TopicCommentMapper topicCommentMapper;

    public Topic selectById(int id) {
        return topicMapper.selectById(id);
    }

    public List<Topic> selectAll() {
        return topicMapper.selectAll();
    }

    public Topic save(Topic topic) {

        int resultCount;
        if (topic.getId() == 0) {
            topic.setCreateTime(new Date());
            topic.setDisplay(TopicEnum.DISTORY_NO.getStatusCode());
            resultCount = topicMapper.save(topic);
        } else {
            resultCount = topicMapper.updateById(topic);
        }

        if (resultCount == 0) {
            return null;
        }
        return topic;
    }

    public int updateById(Topic topic) {
        return topicMapper.updateById(topic);
    }

    public Topic updateDisplay(Topic topic) {
        int resultCount = topicMapper.updateDisplay(topic);
        if (resultCount == 0) {
            return null;
        }
        return this.selectById(topic.getId());
    }

    public Topic updateTop(Topic topic) {
        Topic originTopic = this.selectById(topic.getId());
        if (originTopic.getTop() == TopicEnum.TOP_YES.getStatusCode()) {
            originTopic.setTop(TopicEnum.TOP_NO.getStatusCode());
        } else {
            originTopic.setTop(TopicEnum.TOP_YES.getStatusCode());
        }
        int resultCount = topicMapper.updateById(originTopic);
        if (resultCount == 0) {
            return null;
        }
        return originTopic;
    }

    public List<TopicComment> selectCommentById(Integer topicId) {
        return topicCommentMapper.selectByTopic(topicId);
    }
}
